package com.doraro.platform.module.user.controller;


import com.doraro.platform.common.utils.Result;
import com.doraro.platform.common.utils.ShiroUserUtil;
import com.doraro.platform.module.user.model.entity.SysMenu;
import com.doraro.platform.module.user.model.entity.SysRole;
import com.doraro.platform.module.user.service.ISysMenuService;
import com.doraro.platform.module.user.service.ISysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author doraro
 * @since 2019-05-22
 */
@RestController
@RequestMapping("/api/sysMenu")
@Api(description = "系统菜单", tags = "sysMenu")
public class SysMenuRestController {
    @Autowired
    private ISysMenuService menuService;
    @Autowired
    private ISysRoleService roleService;


    @GetMapping("/list")
    @ApiOperation("获取菜单列表")
    @RequiresPermissions("sysMenu:list")
    public Result getByList() {
        final List<SysMenu> resources = menuService.list();
        return new Result().ok().addData("menus", resources);
    }

    @GetMapping("/nav")
    @ApiOperation("获取当前用户树状菜单列表")
    @RequiresAuthentication
    public Result getNav() {
        final List<SysRole> roles = roleService.getRolesByUserId(ShiroUserUtil.getCurrentId());
        final List<SysMenu> resources = menuService.getTreeList(roles);
        return new Result().ok().addData("menus", resources);
    }

    @GetMapping("/tree")
    @ApiOperation("获取树状菜单列表")
    @RequiresPermissions("sysMenu:tree")
    public Result getByTree() {
        final List<SysMenu> resources = menuService.getTreeList();
        return new Result().ok().addData("menus", resources);
    }

    @PostMapping
    @ApiOperation("创建或更新菜单")
    @RequiresPermissions("sysMenu:createUpdate")
    public Result createMenu(@Validated @RequestBody SysMenu sysMenu) {
        Integer menuId = menuService.createOrUpdateMenu(sysMenu);
        return new Result().ok().addData("id", menuId);
    }

    @DeleteMapping("/{menuId}")
    @ApiOperation("删除菜单")
    @RequiresPermissions("sysMenu:delete")
    public Result createMenu(@PathVariable Integer menuId) {
        menuService.removeMenu(menuId);
        return new Result().ok();
    }


}

